$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/evaluate/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '医生名', name: 'doctorname', index: 'doctorname', width: 80 },
            { label: '用户名', name: 'username', index: 'username', width: 80 },
			{ label: '评论星级', name: 'star', index: 'star', width: 80 },
            { label: '问诊时间', name: 'wenzhentime', index: 'wenzhentime', width: 80 },
			{ label: '是否匿名', name: 'anonymous', index: 'anonymous', width: 80,formatter: function(item, index) {
                    if (item === 0) {
                        return '是';
                    }
                    if (item === 1) {
                        return '否';
                    }
                } },
			{ label: '评论时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.update("+ rowdata.id +")'>" +
                        "详情</a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.del("+ rowdata.id +")'>" +
                        "删除</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            dname:null
        },
		showList: true,
		title: null,
		evaluate: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.evaluate = {};
		},
		update: function (id) {
			vm.showList = false;
            vm.title = "详情";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.evaluate.id == null ? "sys/evaluate/save" : "sys/evaluate/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.evaluate),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (id) {
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/evaluate/delete/"+id,
                        contentType: "application/json",
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){

            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/evaluate/evaluateinfo/"+id,
                success: function(r){
                    vm.evaluate = r.evaluate;
                }
            });
		},
		reload: function (event) {
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            if((startime!=''&&typeof startime!='undefined')&&(endtime!=''&&typeof endtime!='undefined')){
                var st = new Date(startime).getTime();
                var et = new Date(endtime).getTime();
                if(st>et){
                    alert("开始时间不能大于结束时间");
                    return;
                }
            }
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'dname':vm.q.dname,'startime':startime,'endtime':endtime},
                page:page
            }).trigger("reloadGrid");
		}
	}
});